feat: malformed tool call recovery for open-weight models#12231
Draft
roomote-v0[bot] wants to merge 1 commit intomainfrom
Draft
feat: malformed tool call recovery for open-weight models#12231roomote-v0[bot] wants to merge 1 commit intomainfrom
roomote-v0[bot] wants to merge 1 commit intomainfrom
Conversation
When models (especially open-weight models via OpenAI-compatible APIs) emit tool calls as XML markup in their text output instead of using native tool calling, Roo now detects these patterns and provides specific feedback in the retry message. This is non-invasive: it only activates when no native tool calls were detected, and never auto-executes recovered tool calls. The recovered information is used solely to give the model a clearer retry message so it can self-correct. Closes #12185
| // Pattern 2: XML-style <tool_name><param_name>value</param_name></tool_name> | ||
| // Common with some models that try to emulate XML tool calling. | ||
| // Requires at least one underscore in the tool name to avoid matching regular HTML tags. | ||
| const xmlToolPattern = /<([a-z]+_[a-z_]+)>\s*((?:<[a-z_]+>[\s\S]*?<\/[a-z_]+>\s*)+)<\/\1>/i |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related GitHub Issue
Closes: #12185
Description
This PR attempts to address Issue #12185. Feedback and guidance are welcome.
When models (especially open-weight models like qwen3-coder via OpenAI-compatible APIs) emit tool calls as XML markup in their text output instead of using native tool calling, Roo now detects these patterns and provides specific feedback in the retry message so the model can self-correct.
Key implementation details:
New utility (
src/core/task/malformed-tool-call-recovery.ts): Detects common malformed XML-style tool call patterns in assistant text output, including<function=TOOL_NAME><parameter=PARAM_NAME>VALUE</parameter></function>(with or without trailing</tool_call>) and XML-style<tool_name><param>value</param></tool_name>.Enhanced error message (
src/core/prompts/responses.ts):formatResponse.noToolsUsed()now accepts optional malformed tool call info. When detected, the retry message tells the model exactly what XML pattern it tried to use and instructs it to use native tool calling instead.Integration (
src/core/task/Task.ts): In the!didToolUsecode path, before sending the retry message, we scanassistantMessagefor malformed patterns and include recovery info if found.Design choices:
Test Procedure
src/core/task/__tests__/malformed-tool-call-recovery.spec.ts</tool_call>,<tool_call>wrappers, multiple parameters, surrounding text, XML-style patterns, and no-match edge cases (plain text, empty string, regular HTML)cd src && npx vitest run core/task/__tests__/malformed-tool-call-recovery.spec.tsPre-Submission Checklist
Documentation Updates
No documentation updates needed. This is an internal recovery mechanism that improves model retry behavior without changing any user-facing API or configuration.
Interactively review PR in Roo Code Cloud